package cn.leedsoft.luma.test.design_pattern;

/**
 * 抽象工厂模式
 * Created by leeds on 2017/6/14.
 * 1、产品族【热带、亚热带】、产品等级【水果接口、蔬菜接口】
 * 2、工厂接口：针对所有产品接口，一对一提供生产接口
 * 3、具体工厂：针对所有产品族，一对一提供实现工厂
 * 4、具体产品：根据产品族和产品接口，自由组合声明具体产品
 */
public class AbstractFactory {
    public static void main(String[] args) {
        Veggie tv,nv;
        Fruit tf,nf;
        TropicalGardener tg=new TropicalGardener();
        NorthernGardener ng=new NorthernGardener();
        tv=tg.createVeggie("热带菜叶");
        nv=ng.createVeggie("东北甜菜");
        tf=tg.createFruit("海南椰子");
        nf=ng.createFruit("雪梨");
    }
}
//工厂接口：针对所有产品接口，提供生产接口
interface Gardener {
    public Fruit createFruit(String name);
    public Veggie createVeggie(String name);
}

//具体工厂：针对所有产品族，一对一提供实现工厂
class TropicalGardener implements Gardener {
    public Fruit createFruit(String name) {
        return new TropicalFruit(name);
    }
    public Veggie createVeggie(String name) {
        return new TropicalVeggie(name);
    }
}
class NorthernGardener implements Gardener {
    public Fruit createFruit(String name) {
        return new NorthernFruit(name);
    }
    public Veggie createVeggie(String name) {
        return new NorthernVeggie(name);
    }
}

//产品接口：水果、蔬菜
interface Fruit {
}

interface Veggie {
}

//具体产品----产品族和产品接口的自由组合：热带水果、热带蔬菜、亚热带水果、亚热带蔬菜
class TropicalFruit implements Fruit {
    private String name;
    public TropicalFruit(String name) {
        System.out.println("热带工厂为您创建了：热带水果－"+name);
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

class TropicalVeggie implements Veggie {
    private String name;
    public TropicalVeggie(String name) {
        System.out.println("热带工厂为您创建了：热带水果－"+name);
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

class NorthernFruit implements Fruit {
    private String name;
    public NorthernFruit(String name) {
        System.out.println("亚热带工厂为您创建了：亚热带水果－"+name);
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

class NorthernVeggie implements Veggie {
    private String name;
    public NorthernVeggie(String name) {
        System.out.println("亚热带工厂为您创建了：亚热带蔬菜－"+name);
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}



